---
title: "QCA Rel Reg - S2 - QCA2 - Outcome 2 - NXX"
author: "Sannehag & Ettensperger"
date: '`r Sys.Date()`'
output:
  html_document: default
  pdf_document: default
---

<style type="text/css">
.main-container {
  max-width: 1800px;
  margin-left: auto;
  margin-right: auto;
}
</style>

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## set working directory & load data

```{r}
setwd("E:/Fortis/Workspace/Research Article - QCA Religious Regulation")
# setwd("C:/Users/fe300/Desktop/Research Article - Reg Rel QCA")


QCA.Data <- read.csv("Data_QCA2_nxx.csv")
```

#Load packages for QCA

```{r}
library(QCA)
library(SetMethods)
library(ggplot2)
```

```{r}
# set names for truth table
QCA.Data$name_vdem -> rownames(QCA.Data)
```

## Calibration details


```{r, dpi=200}
rel.CAL <- plot(QCA.Data$al_religion2000, QCA.Data$qca.relfrag, xlab = "Religious fractionalization (raw)",
             ylab = "Religious fractionalization (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

rel.CAL

gdp.CAL <- plot(QCA.Data$mad_gdppc, QCA.Data$qca.gdp, xlab = "GDP per capita (raw)",
                ylab = "GDP per capita (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 12500, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

gdp.CAL

pref.CAL <- plot(QCA.Data$Preferred_Recoded, QCA.Data$qca.pref1980, xlab = "Preference for state religion (raw)",
                ylab = "Preference for state religion (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 2.05, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

pref.CAL

highc.CAL <- plot(QCA.Data$vdem_20ya_juhcind, QCA.Data$qca.highcourt, xlab = "High Court Indipendence (20 year average, raw)",
                        ylab = "High Court Indipendence (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

highc.CAL

soccomlegit.CAL <- plot(QCA.Data$vdem_20ya_legideo_1, QCA.Data$qca.soccomlegit, xlab = "Socialist/Communist legitimacy (20 year average, raw)",
                        ylab = "Socialist/Communist legitimacy (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.201, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

soccomlegit.CAL


rellegit.CAL <- plot(QCA.Data$vdem_20ya_legideo_4, QCA.Data$qca.rellegit, xlab = "Religious legitimacy (20 year average, raw)",
                        ylab = "Religious legitimacy (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.201, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

rellegit.CAL




## outcome

rel.reg.CAL <- plot(QCA.Data$NXX2014X, QCA.Data$qca.outcome2.nxx, xlab = "Religious regulation - Majority Rel. and all reg. (raw)",
                     ylab = "Religious regulation - Majority Rel. and all reg. (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 10.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

rel.reg.CAL

```


```{r}
### NCA - Necessary Condition Analysis
QCAfit(QCA.Data[, c(17:18,23:26)], QCA.Data$qca.outcome2.nxx, necessity = TRUE)
QCAfit(QCA.Data[, c(17:18,23:26)], 1 - QCA.Data$qca.outcome2.nxx, necessity = TRUE)
```


```{r, fig.height=7, fig.width=9, dpi=200}
### correlation of calibrated data
corrplot::corrplot.mixed(cor(QCA.Data[, c(17:18,23:27)]), lower="number", upper="circle", tl.pos = "lt")

corrplot::corrplot.mixed(cor(QCA.Data[, c(17:27)]), lower="number", upper="circle", tl.pos = "lt")


```




```{r}
TruthTable.pars <- truthTable(QCA.Data[, c(17:18,23:27)], outcome = "qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.pref1980, qca.highcourt, qca.soccomlegit, qca.rellegit",
                    complete = TRUE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.75)
## show truth table
TruthTable.pars
```



######## solution terms pars. sol.

```{r}
solution.ps <- minimize(TruthTable.pars, outcome = "qca.outcome2.nxx", include = "1, ?", row.dom = TRUE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    method = "CCubes")

## parsimonious solution (Table XX) 
solution.ps

solution.ps$PIchart
```




### ---------- new truthtable

```{r}
TruthTable.nonoutcome.pars <- truthTable(QCA.Data[, c(17:18,23:27)], outcome = "~qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.pref1980, qca.highcourt, qca.soccomlegit, qca.rellegit",
                    complete = TRUE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.90)
## show truth table
TruthTable.nonoutcome.pars
```


### ----------


# Non-outcome parsimonious solution (0.90 inclusion)

```{r}
non.solution.ps <- minimize(TruthTable.nonoutcome.pars, outcome = "~qca.outcome2.nxx", include = "1, ?", row.dom = TRUE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    method = "CCubes")

## parsimonious non-oucome solution (Table XX) 
non.solution.ps

non.solution.ps$PIchart
```




### ------------------------
### ------------------------


### conservative solution - truth table

```{r}
TruthTable.cons <- truthTable(QCA.Data[, c(17:18,23:27)], outcome = "qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.pref1980, qca.highcourt, qca.soccomlegit, qca.rellegit",
                    complete = FALSE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.75)

# Truth table conservative
TruthTable.cons
```

## conservative solution
## outcome

```{r}
solution.cs <- minimize(TruthTable.cons, outcome = "qca.outcome2.nxx",include = "1", row.dom = FALSE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    all.sol = TRUE, method = "CCubes")

solution.cs

solution.cs$PIchart
```




### non-outcome conservative solution - 0.9

```{r}
TruthTable.negout.cons <- truthTable(QCA.Data[, c(17:18,23:27)], outcome = "~qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.pref1980, qca.highcourt, qca.soccomlegit, qca.rellegit",
                    complete = FALSE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.9)

# Truth table conservative 0.9
TruthTable.negout.cons

```




```{r, eval=TRUE}
non.solution.cs <- minimize(TruthTable.negout.cons, outcome = "~qca.outcome2.nxx",include = "1", row.dom = FALSE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    all.sol = TRUE, method = "CCubes")

non.solution.cs

non.solution.cs$PIchart
```

## fin

